'use strict';

stareal
    .controller("IndexSearchController", function ($scope, $api, $stateParams, $timeout, $lazyLoader,localStorageService,$alert) {
        // 展现热词
        $scope.hwf = true;
        $scope.change_navBarIndex=1;
        var h=$(window).height()-2.2*parseFloat(document.documentElement.style.fontSize)+"px";
        $("#wrapper_container").height(h);
        //历史搜索
        $scope.history_list1 =new Array(8);
        if(localStorageService.get("Indexpricesearchlist")!=undefined&&localStorageService.get("Indexpricesearchlist")!=null&&localStorageService.get("Indexpricesearchlist")!=''){
            $scope.history_list1 = JSON.parse(localStorageService.get("Indexpricesearchlist"));
        }else{
            $scope.history_list1=[];
        }
        if($scope.history_list1){
            $scope.history1 = $scope.history_list1;
        }else{
            $scope.history1 = [];
        }
        // console.log($scope.history)
        // $scope.history_list;
        //搜索方法
        var search1 = function(keyword){
            $timeout(function () {
                $scope.hwf = false;
                $scope.hwe = false;
                $scope.goods1 = new $lazyLoader("app/good/classifyid", {
                    name: keyword
                });
                $scope.goods1.nextPage();
            },0);
        };
        //点击热词搜索
        $scope.hws1 = function(keyword){
            $scope.keyword1= keyword;
            search1(keyword);
        };
       // $scope.history_list1 = JSON.parse(localStorageService.get("pricesearchlist"));
        //回车搜索

        var searchBtn1 = document.getElementById("searchBtn1");
        var sou_iocnBtn1=document.getElementById('sou_iocnBtn1');
        var searchClear_icon1=document.getElementById('searchClear_icon1');
        searchBtn1.onsearch = function(event){
            event.preventDefault();
            var keyword = this.value;

            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search1(keyword);
                $scope.history1.unshift({"word": keyword});
                localStorageService.set("Indexpricesearchlist", JSON.stringify($scope.history1));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        };
        sou_iocnBtn1.onclick=function(){
            var keyword = $('#searchBtn1').val();
            if(keyword!=''&&keyword!=null&&keyword!=undefined){
                search1(keyword);
                $scope.history1.unshift({"word":keyword});
                localStorageService.set("Indexpricesearchlist",JSON.stringify($scope.history1));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }

        }
        searchClear_icon1.onclick=function(){
            $('#searchBtn1').val('');
        }
        //输入没有内容
        searchBtn1.oninput = function () {
            if(this.value.length<1){
                $scope.hwe = true;
                $scope.hwf = true;
                $scope.goods1.nodata=false;
                $scope.history_list1 = JSON.parse(localStorageService.get("Indexpricesearchlist"));
            }
        }
        $scope.dele1 = function (id) {
            $scope.history_list1.splice(id,1 );
            $scope.history1.splice(id,1 );
            localStorageService.set("Indexpricesearchlist",JSON.stringify($scope.history_list1));
        }
        $scope.clearAll1=function(){
            $scope.history_list1=[];
            $scope.history1=[];
            localStorageService.set("Indexpricesearchlist",'');
        }
        // localStorageService.remove("list")

        //历史搜索
        $scope.history_list2 =new Array(8);
        if(localStorageService.get("Indexviewsearchlist")!=undefined&&localStorageService.get("Indexviewsearchlist")!=null&&localStorageService.get("Indexviewsearchlist")!='') {
            $scope.history_list2 = JSON.parse(localStorageService.get("Indexviewsearchlist"));
        }else{
            $scope.history_list2=[];
        }
        if($scope.history_list2){
            $scope.history2 = $scope.history_list2;
        }else{
            $scope.history2 = [];
        }
        // console.log($scope.history)
        // $scope.history_list;
        //搜索方法
        var search2 = function(keyword){
            $timeout(function () {
                $scope.hwf = false;
                $scope.hwe = false;
                $scope.goods2 = new $lazyLoader("app/view/searchlist", {
                    name: keyword,
                    flag:1
                });
                $scope.goods2.nextPage();
            }, 0);
        };
        //点击热词搜索
        $scope.hws2 = function(keyword){
            $scope.keyword2 = keyword;
            search2(keyword);
        };
       // $scope.history_list2 = JSON.parse(localStorageService.get("viewsearchlist"));
        //回车搜索
        var searchBtn2 = document.getElementById("searchBtn2");
        var sou_iocnBtn2=document.getElementById('sou_iocnBtn2');
        var searchClear_icon2=document.getElementById('searchClear_icon2');
        searchBtn2.onsearch = function(event){
            event.preventDefault();
            var keyword = this.value;
            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search2(keyword);
                $scope.history2.unshift({"word": keyword});
                localStorageService.set("Indexviewsearchlist", JSON.stringify($scope.history2));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        };
        sou_iocnBtn2.onclick=function(){
            var keyword = $('#searchBtn2').val();

            if(keyword!=''&&keyword!=null&&keyword!=undefined) {
                search2(keyword);
                $scope.history2.unshift({"word": keyword});
                localStorageService.set("Indexviewsearchlist", JSON.stringify($scope.history2));
            }else{
                $timeout(function () {
                    $alert.show('搜索内容不能为空!');
                },0)
            }
        }
        searchClear_icon2.onclick=function(){
            $('#searchBtn2').val('');
        }
        //输入没有内容
        searchBtn2.oninput = function () {
            if(this.value.length<1){
                $scope.hwe = true;
                $scope.hwf = true;
                $scope.goods2.nodata = false;
                $scope.history_list2 = JSON.parse(localStorageService.get("Indexviewsearchlist"));
            }
        }
        $scope.dele2 = function (id) {
            $scope.history_list2.splice(id,1 );
            $scope.history2.splice(id,1 );
            localStorageService.set("Indexviewsearchlist",JSON.stringify($scope.history_list2));
        }
        $scope.clearAll2=function(){
            $scope.history_list2=[];
            $scope.history2=[];
            localStorageService.set("Indexviewsearchlist",'');
        }
        //切换
        $scope.switchNavBar=function(index){
            $scope.change_navBarIndex=index;
        }
    });